﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DbAdapter.sqldbAdapter;

namespace Water.Model
{
    /// <summary>
    /// Tbt_SitMap 菜单表
    /// </summary>
    public class MenuTree
    {
        public virtual int SID { get; set; }
        public virtual string URL { get; set; }
        public virtual string Title { get; set; }
        public virtual string Description { get; set; }
        public virtual string NodeID { get; set; }
        public virtual string PNodeID { get; set; }
        public virtual string ImageUrl { get; set; }

        public IList<MenuTree> GetMenutreeForUser(string uname)
        {
            sqldb db = new sqldb();
            string sql = "select * from Tbt_SitMap b "
                        + "where Exists(select * from Tbt_UserAndMenu a where a.UserName = '"
                        + uname + "' and a.SID = b.NodeID)";
            DataSet ds = db.ExecuteDataset(sqlconn.GetConnStr(), CommandType.Text, sql);
            IList<MenuTree> menulist = null;
            if (ds != null && ds.Tables[0] != null && ds.Tables[0].Rows.Count > 0)
            {
                menulist = new List<MenuTree>();
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    MenuTree objmenu = new MenuTree();
                    objmenu.SID = Convert.ToInt32(dr["SID"]);
                    objmenu.URL = dr["URL"].ToString();
                    objmenu.Title = dr["Title"].ToString();
                    objmenu.Description = dr["Description"].ToString();
                    objmenu.NodeID = dr["NodeID"].ToString();
                    objmenu.PNodeID = dr["PNodeID"].ToString();
                    objmenu.ImageUrl = dr["ImageUrl"].ToString();
                    menulist.Add(objmenu);
                }
            }
            return menulist;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="Vparam"></param>
        /// <returns></returns>
        public IList<MenuTree> GetAllMenuTrees(int pageIndex, int pageSize)
        {
            int startrow = pageSize * (pageIndex - 1);
            int endrow = pageSize * pageIndex + 1;
            sqldb db = new sqldb();
            string query = "SELECT * FROM Tbt_SitMap order by NodeID";// (select ROW_NUMBER() OVER(ORDER BY F.NodeID ASC) rownumber,F.* "
                          //+ "FROM Tbt_SitMap F) A WHERE A.rownumber > " + startrow.ToString() + " AND A.rownumber < "
                          //+ endrow.ToString();
            DataSet ds = db.ExecuteDataset(sqlconn.GetConnStr(), CommandType.Text, query);
            IList<MenuTree> menulist = null;
            if (ds != null && ds.Tables[0] != null && ds.Tables[0].Rows.Count > 0)
            {
                menulist = new List<MenuTree>();
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    MenuTree objmenu = new MenuTree();
                    objmenu.SID = Convert.ToInt32(dr["SID"]);
                    objmenu.URL = dr["URL"].ToString();
                    objmenu.Title = dr["Title"].ToString();
                    objmenu.Description = dr["Description"].ToString();
                    objmenu.NodeID = dr["NodeID"].ToString();
                    objmenu.PNodeID = dr["PNodeID"].ToString();
                    objmenu.ImageUrl = dr["ImageUrl"].ToString();
                    menulist.Add(objmenu);
                }
            }
            return menulist;
        } 
    }
}
